<?php
class UsersController extends AppController {

	var $name = 'Users';
	//var $helpers = array('Html', 'Form');
	var $components = array('Session','Cookie',
	'Auth' => array(
		'authorize' => 'actions',
		'actionPath' => 'controllers/',
		'loginAction' => array(
		'controller' => 'users',
		'action' => 'login',
		'plugin' => false,
		'admin' => false,
	),
	),
	);
	function beforeFilter() {
		/*
		 *$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		 $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
		 $this->Auth->allow('display');
		 $this->Auth->authorize = 'controller';
		 */
		//$this->__validateLoginStatus();
		$this->Auth->fields = array(
		'username' => 'name',
		'password' => 'password'
		);
		//$this->Auth->authenticate = ClassRegistry::init('User');
		//$this->Auth->allow('index','view','register','logout');
		$this->Auth->allow('*');
		//$this->Auth->loginRedirect = array('controller'=>'users', 'action'=>'index');
	}
	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		else $this->data['User'] = null;
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	/**
	 * The AuthComponent provides the needed functionality
	 * for login, so you can leave this function blank.
	 */
	function login() {
		$this->Auth->loginError = "This message shows up when the wrong credentials are used";
		$this->Auth->authError = "This error shows up with the user tries to access a part of the website that is protected.";
		//-- code inside this function will execute only when autoRedirect was set to false (i.e. in a beforeFilter).
		if(empty($this->data) == false)
		{
			if(($user = $this->User->validateLogin($this->data['User'])) == true)
			{
				//debug($user);
				//debug($this->Session->read('User'));
				$this->Session->write('User', $user);
				$this->Session->setFlash('You\'ve successfully logged in.');
				$this->redirect('index');
			}
			else
			{
				$this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
			}
		}
		/*		if ($this->Auth->user()) {
			if (!empty($this->data['User']['remember_me'])) {
			$cookie = array();
			$cookie['username'] = $this->data['User']['username'];
			$cookie['password'] = $this->data['User']['password'];
			$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
			unset($this->data['User']['remember_me']);
			}
			$this->redirect(array('action' => 'index'));
			//$this->redirect($this->Auth->redirect());
			}
			if (empty($this->data)) {
			$cookie = $this->Cookie->read('Auth.User');
			if (!is_null($cookie)) {
			if ($this->Auth->login($cookie)) {
			// Clear auth message, just in case we use it.
			$this->Session->delete('Message.auth');
			//$this->redirect($this->Auth->redirect());
			$this->redirect(array('action' => 'index'));
			}
			}
			}
			*/
	}
	function logout() {
		$this->Session->destroy();
		$this->redirect($this->Auth->logout());
		/*
		 *         $this->Session->destroy('user');
		 $this->Session->setFlash('You\'ve successfully logged out.');
		 $this->redirect('login');
		 */
	}
	function register() {
		if(!empty($this->data)) {
			$this->User->create();
			if($this->data['User']['password'] == $this->data['User']['password_confirm']){
				$this->data['User']['password']=md5($this->data['User']['password_confirm']);
			}
			if($this->User->save($this->data)) {
				// send signup email containing password to the user
				$this->Auth->login($this->data);
				$this->redirect('index');
			}
		}
	}
	function __validateLoginStatus()
	{
		//debug($this->Session->read('User'));
		if($this->action != 'login' && $this->action != 'logout')
		{
			if(!$this->Session->check('User'))
			{
				$this->redirect('login');
				$this->Session->setFlash('The URL you\'ve followed requires you login.');
			}
		}
	}
}
